/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.biz.rest;

import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.biz.domain.Announcement;
import me.zhengjie.modules.biz.service.AnnouncementService;
import me.zhengjie.modules.biz.service.dto.AnnouncementQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://eladmin.vip
* @author Admin
* @date 2023-02-17
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "announcement管理")
@RequestMapping("/api/announcement")
public class AnnouncementController {

    private final AnnouncementService announcementService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check('announcement:list')")
    public void exportAnnouncement(HttpServletResponse response, AnnouncementQueryCriteria criteria) throws IOException {
        announcementService.download(announcementService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询announcement")
    @ApiOperation("查询announcement")
//    @PreAuthorize("@el.check('announcement:list')")
    public ResponseEntity<Object> queryAnnouncement(AnnouncementQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(announcementService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增announcement")
    @ApiOperation("新增announcement")
//    @PreAuthorize("@el.check('announcement:add')")
    public ResponseEntity<Object> createAnnouncement(@Validated @RequestBody Announcement resources){
        return new ResponseEntity<>(announcementService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改announcement")
    @ApiOperation("修改announcement")
//    @PreAuthorize("@el.check('announcement:edit')")
    public ResponseEntity<Object> updateAnnouncement(@Validated @RequestBody Announcement resources){
        announcementService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除announcement")
    @ApiOperation("删除announcement")
//    @PreAuthorize("@el.check('announcement:del')")
    public ResponseEntity<Object> deleteAnnouncement(@RequestBody Integer[] ids) {
        announcementService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @AnonymousAccess
    @GetMapping("/intro")
    @Log("平台介绍")
    @ApiOperation("查询平台介绍")
    public ResponseEntity<Object> queryPlatformIntro(){
        return new ResponseEntity<>(announcementService.queryPlatformIntro(),HttpStatus.OK);
    }

    @AnonymousAccess
    @GetMapping("/notes")
    @Log("查询公告列表")
    @ApiOperation("查询公告列表")
    public ResponseEntity<Object> queryAnnouncementList(AnnouncementQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(announcementService.queryAnnouncementList(criteria,pageable),HttpStatus.OK);
    }

    @AnonymousAccess
    @GetMapping("/news")
    @Log("查询新闻信息列表")
    @ApiOperation("查询新闻信息列表")
    public ResponseEntity<Object> queryNewsList(AnnouncementQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(announcementService.queryNewsList(criteria,pageable),HttpStatus.OK);
    }

    @AnonymousAccess
    @GetMapping("/hcsmng")
    @Log("查询规章制度列表")
    @ApiOperation("查询规章制度列表")
    public ResponseEntity<Object> queryRulesList(AnnouncementQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(announcementService.queryRulesList(criteria,pageable),HttpStatus.OK);
    }

    @AnonymousAccess
    @GetMapping("/rechargeinfo")
    @Log("查询收费标准列表")
    @ApiOperation("查询收费标准列表")
    public ResponseEntity<Object> queryChargeList(AnnouncementQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(announcementService.queryChargeList(criteria,pageable),HttpStatus.OK);
    }

    @AnonymousAccess
    @GetMapping("/notesDetail")
    @Log("查询新闻详情")
    @ApiOperation("查询新闻详情")
    public ResponseEntity<Object> queryAnnouncementListDetail(@RequestParam Integer id){
        return new ResponseEntity<>(announcementService.queryAnnouncementListDetail(id),HttpStatus.OK);
    }

    @AnonymousAccess
    @GetMapping("/announcmentList")
    @ApiOperation("查询政策和规则")
    public ResponseEntity<Object> queryAnnouncmentList(AnnouncementQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(announcementService.queryAnnouncmentList(criteria,pageable),HttpStatus.OK);
    }

    @AnonymousAccess
    @GetMapping("/policyList")
    @ApiOperation("政策列表")
    public ResponseEntity<Object> queryPolicyList(AnnouncementQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(announcementService.queryPolicyList(criteria,pageable),HttpStatus.OK);
    }

    @AnonymousAccess
    @GetMapping("/ruleList")
    public ResponseEntity<Object> queryRuleList(AnnouncementQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(announcementService.queryRuleList(criteria,pageable),HttpStatus.OK);
    }
}
